﻿#include "NativeEvent.h"
#include "Windows.h"
#include <QDebug>
#include <QTimer>
#include <Dbt.h>

NativeEvent::NativeEvent()
{
    m_timer = new QTimer;
    m_timer->setSingleShot(true);
    connect(m_timer,&QTimer::timeout,[this](){
        emit devUpdate();
    });
}

NativeEvent::~NativeEvent()
{
    m_timer->deleteLater();
}

bool NativeEvent::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{
    MSG *msg = reinterpret_cast<MSG*>(message);
    int msgType = msg->message;

    if (msgType == WM_DEVICECHANGE) {
        switch (msg->wParam) {
        case DBT_DEVICEARRIVAL:
//            qDebug() << "arrival";
            break;
        case DBT_DEVICEREMOVECOMPLETE:
//            qDebug() << "remove";
            break;
        case DBT_DEVNODES_CHANGED:
//            qDebug() << "DBT_DEVNODES_CHANGED";
            m_timer->start(1500);
            break;
        case DBT_DEVTYP_DEVNODE:
//            qDebug() << "DBT_DEVTYP_DEVNODE";
            break;
        default:
            break;
        }
    }
    return QWidget::nativeEvent(eventType,message,result);
}
